package com.fr.plugin;

import com.fr.decision.plugin.db.AbstractDecisionDBAccessProvider;
import com.fr.plugin.dao.MyDao;
import com.fr.plugin.entity.MyEntity;
import com.fr.plugin.transform.ExecuteFunctionRecord;
import com.fr.plugin.transform.FunctionRecorder;
import com.fr.stable.db.accessor.DBAccessor;
import com.fr.stable.db.dao.BaseDAO;
import com.fr.stable.db.dao.DAOProvider;

// 1.继承AbstractDecisionDBAccessProvider
@FunctionRecorder
public class MyDecisionDBAccessProvider extends AbstractDecisionDBAccessProvider {

    // 2.注册数据库访问类
    @Override
    @ExecuteFunctionRecord
    public DAOProvider[] registerDAO() {
        return new DAOProvider[]{
                // 匿名类
                new DAOProvider() {
                    @Override
                    public Class getEntityClass() {
                        return MyEntity.class;
                    }

                    @Override
                    public Class<? extends BaseDAO> getDAOClass() {
                        return MyDao.class;
                    }
                }
        };
    }
    // 3.新建一个DBAccessor静态变量
    private static DBAccessor dbAccessor = null;

    // 4.写一个公共的静态方法获取DBAccessor
    public static DBAccessor getDbAccessor() {return dbAccessor;}

    // 5.重写数据库访问器注入函数（当数据库访问器准备好之后又fr注入到这里）
    @Override
    public void onDBAvailable(DBAccessor dbAccessor) {
        MyDecisionDBAccessProvider.dbAccessor = dbAccessor;
    }
}
